﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Notes.Resources;
using NotesAgent;

namespace Notes
{
    public partial class RecoverPasswordPage : PhoneApplicationPage
    {
        bool working = false;
        public RecoverPasswordPage()
        {
            InitializeComponent();
        }

        private void CancelUpdate_Button_Click(object sender, RoutedEventArgs e)
        {
            ToggleOverlay(false);
        }
        public void OnError(string message)
        {
            Dispatcher.BeginInvoke(() =>
            {
                Username_Area.IsEnabled = true;
                working = false;
                ToggleOverlay(false);
                working = false;
                DisableProgressBar();
                MessageBox.Show(message);
            });
        }
        public void ToggleOverlay(bool isOpen)
        {
            working = isOpen;
            if (isOpen)
            {
                //PageTitle.Text = Strings.Working;
                //Current_Step_Label.Visibility = System.Windows.Visibility.Visible;
                CancelUpdate_Button.Visibility = System.Windows.Visibility.Visible;
                RemainingTime_Label.Visibility = System.Windows.Visibility.Visible;
                EnableProgressBar();

                CheckUserPassword_Button.Visibility = System.Windows.Visibility.Collapsed;

                Username_Text.Visibility = System.Windows.Visibility.Collapsed;
                Username_Area.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                DisableProgressBar();
                Username_Text.Visibility = System.Windows.Visibility.Visible;
                Username_Area.Visibility = System.Windows.Visibility.Visible;
                CheckUserPassword_Button.Visibility = System.Windows.Visibility.Visible;
                CancelUpdate_Button.Visibility = System.Windows.Visibility.Collapsed;
            }
            
        }
        private void EnableProgressBar()
        {
            Dispatcher.BeginInvoke(() =>
            {
                ToggleProgressBar.IsEnabled = true;
                ToggleProgressBar.IsIndeterminate = true;
                ToggleProgressBar.Visibility = System.Windows.Visibility.Visible;
            });
        }
        private void DisableProgressBar()
        {
            Dispatcher.BeginInvoke(() =>
            {
                ToggleProgressBar.IsEnabled = false;
                ToggleProgressBar.IsIndeterminate = false;
                ToggleProgressBar.Visibility = System.Windows.Visibility.Collapsed;
            });
        }
        private void CheckUserPassword_Button_Click(object sender, RoutedEventArgs e)
        {
            working = true;

            if (Username_Area.Text == "")
            {
                OnError(Strings.MissingData);
                return;
            }
            
            ToggleOverlay(true);
            RecoverPassword();
        }

        public void RecoverPassword()
        {
            string url = "http://fboscari.altervista.org/Notes/sendmail_phone.php?";
            //url += Utils.GetUpdateLiveTileArgs(result, currentAccount.contract, currentAccount.username, currentAccount.password, currentAccount.number, currentAccount.showDate);
            url += "username=" + Username_Area.Text + "&lang=" + Strings.Lang + "&salt=" + DateTime.Now.Millisecond;
            Storage.getHTML(url, s =>
            {
                Dispatcher.BeginInvoke(() =>
                {
                    ToggleOverlay(false);
                    if (s == "-2")
                    {
                        OnError(Strings.MissingData);
                        return;
                    }
                    else if (s == "-1")
                    {
                        OnError(Strings.UnknownEmail);
                        return;
                    }
                    else
                        OnError(Strings.EmailSent);
                    NavigationService.GoBack();
                });
            },
            (e) =>
            {
                OnError(Strings.ConnectionError);

            });
        }

        
        private void Username_Area_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                
                CheckUserPassword_Button.Focus();
                CheckUserPassword_Button_Click(null, null);

            }
        }
    }
}